<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html lang="zh">
<head>
<%@include file="/WEB-INF/jsp/common/header.jsp"%>
<style type="text/css">
    td ul.demo-btns i {color: #e5e5e5;padding: 0 10px;font-style: normal;}
    td ul.demo-btns a.editATxts, a.editATxts {color:#6eb1d4;}
    td ul.demo-btns a.disabledATxts {color:#8c8c8c;}
    td ul.demo-btns a.deleteATxts {color:#ff6969;}
    td ul.demo-btns a.infoATxts {color:#2e9f76;}
</style>
<script type="text/javascript">
	var petListTable;
	$(document).ready(function() {
		$("#resetForm").click(function() {
			$("#form input").val("");
		});
		$("#save").click(function() {
			var id = $(".modal-body input[name=id]").val();
			var name = $(".modal-body input[name=name]").val();
            var foods = $(".modal-body input[name=foods]").val();
            var remark = $(".modal-body input[name=remark]").val();

			if ($.trim(name) === "") {
				$.messager.alert("房间名不能为空");
				return false;
			}
			var url = contextpath + "/web/pet/update";
            if(id === ''){
                url = contextpath + "/web/pet/insert";
            }
			$(this).attr("disabled","disabled");
			$.ajax({
				type : "POST",
				url : url,
				data : $('#editForm').serialize(),
				success : function(result) {
                    if (result.code === 200) {
                        $("#save").removeAttr("disabled");
                        $("#editModal").modal("hide");
                        $.messager.alert("提示", "保存成功");
                        petListTable.ajax.reload(null, false);
                    } else {
                        $.messager.alert("数据保存失败", result.msg);
                    }
				}
			});
		});
        petListTable = $('#petListTable').DataTable({
            "bSort": true, //是否启动各个字段的排序功能
            "aaSorting": [[5, "desc"]], //默认的排序方式
            "aoColumns": [
                {
                    "mDataProp": "avatar",
                    "sTitle":"头像",
                    "sDefaultContent": "--",
                    "sWidth": "80px",
                    "sClass": "center flow",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : true,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData !== '--'){
                            $(nTd).html('<img src="'+ sData +'" alt="--" width="32" height="32">');
                        }
                    }
                },
                {
                    "mDataProp": "name",
                    "sTitle":"名称",
                    "sDefaultContent": "--",
                    "sWidth": "15%",
                    "sClass": "center flow",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData !== '--') $(nTd).attr('title',sData);
                    }
                },
                {
                    "mDataProp": "userName",
                    "sTitle":"主人",
                    "sDefaultContent": "--",
                    "sWidth": "15%",
                    "sClass": "center",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData !== '--') $(nTd).attr('title',sData);
                    }
                },
                {
                    "mDataProp": "foods",
                    "sTitle":"爱好食品",
                    "sDefaultContent": "--",
                    "sWidth": "20%",
                    "sClass": "left",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData !== '--') {
                            $(nTd).attr('title', sData);
                        }
                    }
                },
                {
                    "mDataProp": "remark",
                    "sTitle":"备注",
                    "sDefaultContent": "--",
                    "sWidth": "20%",
                    "sClass": "left",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData !== '--') {
                            $(nTd).attr('title', sData);
                        }
                    }
                },
                {
                    "mDataProp": "createTime",
                    "sTitle": "创建时间",
                    "sWidth": "15%",
                    "sDefaultContent": "--",
                    "bSortable": true,
                    "sClass": "center flow",
                    "bSearchable": false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if (sData !== '--') $(nTd).attr('title', sData);
                    }
                },
                {
                    "mDataProp": "id",
                    "sTitle": "操作",
                    "sWidth": "175px",
                    "sClass": "center flow",
                    "bSearchable": false,
                    "bSortable": false,
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        $(nTd).html("<a class='btn btn-xs btn-default' href='javascript:void(0);' onclick='editPet(" + iRow + ")'><i class='fa fa-pencil'></i>&nbsp;编辑</a>&nbsp;&nbsp;")
                            .append("<a class='btn btn-xs btn-default' href='javascript:void(0);' onclick='deletePet(" + iRow + ")'><i class='fa fa-times'></i>&nbsp;删除</a>&nbsp;&nbsp;")
                            .append("<a class='btn btn-xs btn-default' href='${pageContext.request.contextPath}/web/pet/service/" + sData + "'><i class='fa fa-level-down'></i>&nbsp;更多</a>&nbsp;&nbsp;");
                    }
                }
            ],
            "ajax": {
                url: contextpath + '/web/pet/queryPage',
                type:"post",
                data: function(data){
                    $.xDataTablesParams(data, $.trim($('#name').val()));
                    return data;
                }
            }
        });

        $('#editModal').on('hide.bs.modal', function () {
            $('#editForm input').val('');
            $('#save').removeAttr("disabled");
        });
    });

    function editPet(row){
        $('#modalTitle').text('编辑宠物信息');
        var rowData = petListTable.row(row).data();
        if(!rowData.price) rowData.price = 0;
        $('#editModal :input[name=id]').val(rowData.id);
        $('#editModal :input[name=name]').val(rowData.name);
        $('#editModal :input[name=foods]').val(rowData.foods);
        $('#editModal :input[name=remark]').val(rowData.remark);
        $('#editModal').modal('show');
    }

    function deletePet(row){
        var rowData = petListTable.row(row).data();
        $.messager.confirm("确定", "真的要删除'" + rowData.name + "'吗？", function(){
            $.ajax({
               url : contextpath + '/web/pet/delete/' + rowData.id,
               type : 'DELETE',
               success :  function(data){
                   if (data.code === 200) {
                       $.messager.alert("提示", "删除成功");
                       petListTable.ajax.reload();
                   } else {
                       $.messager.alert(data.msg);
                   }
               }
            });
        });
    }

    function searchTable(){
        petListTable.ajax.reload();
    }

</script>
</head>
<body>
	<div class="container-fluid">
		<div class="sub-header">
			<h2>宠物列表</h2>
		</div>
		<form id="form" class="form-inline" role="form" action="#" method="post" style="margin-bottom: 10px; margin-top: 10px;">
			<div class="form-group">
				<div class="input-group">
					<span class="input-group-addon">名称</span>
					<input type="text" class="form-control" id="name" placeholder="名称" style="width: 150px;">
				</div>
			</div>
			<button type="button" class="btn btn-default" onclick="searchTable()">查询</button>
		</form>
		<div>
			<table class="table table-hover table-striped table-bordered" style="table-layout:fixed;" id="petListTable"></table>
		</div>
	</div>

	<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-hidden="true">
		<div class="modal-dialog modal-lg" style="width: 600px;">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">
						<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
					</button>
					<h4 class="modal-title" id="modalTitle">宠物信息</h4>
				</div>
				<div class="modal-body">
                    <form id="editForm">
                        <input type="hidden" name="id">
                        <table id="editTable" class="table-hover table table-bordered">
                            <tr>
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon addForm">名称</span>
                                        <input type="text" class="form-control" name="name" placeholder="名称" style="width: 320px;">
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon">食物</span>
                                        <textarea class="form-control" name="foods" rows="3" placeholder="喜欢的食物品牌"></textarea>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon">备注</span>
                                        <textarea class="form-control" name="remark" rows="3" placeholder="备注"></textarea>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td align="center">
                                    <button id="save" type="button" class="btn btn-success addForm">保 存</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    <button id="closeModal" type="button" onclick="$('#editModal').modal('hide');" class="btn btn-danger addForm">关闭</button>
                                </td>
                            </tr>
                        </table>
                    </form>
				</div>
			</div>
		</div>
	</div>


</body>